package com.yxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yxy.common.PageResult;
import com.yxy.constant.StatusConstant;
import com.yxy.context.BaseContext;
import com.yxy.mapper.RemarkMapper;
import com.yxy.mapper.RemarkThumbsMapper;
import com.yxy.mapper.UserMapper;
import com.yxy.pojo.dto.RemarkDTO;
import com.yxy.pojo.dto.RemarkPageQueryDTO;
import com.yxy.pojo.entity.RemarkThumbs;
import com.yxy.pojo.entity.Remark;
import com.yxy.pojo.entity.User;
import com.yxy.pojo.vo.RemarkPageQueryVO;
import com.yxy.service.RemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class RemarkServiceImpl implements RemarkService {
    @Autowired
    private RemarkMapper remarkMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RemarkThumbsMapper remarkThumbsMapper;

    /**
     * 添加评论
     *
     * @param remarkDTO
     */
    @Transactional
    @Override
    public void add(RemarkDTO remarkDTO) {
        // 添加评论
        Remark remark = Remark.builder()
                .userId(remarkDTO.getUserId())
                .typeId(remarkDTO.getTypeId())
                .type(remarkDTO.getType())
                .content(remarkDTO.getContent())
                .remarkTime(LocalDateTime.now())
                .build();
        remarkMapper.add(remark);
        // 添加评论对应的赞，初始化为-1
        RemarkThumbs remarkThumbs = RemarkThumbs.builder()
                .userId(remarkDTO.getUserId())
                .remarkId(remark.getId())
                .isThumbs(StatusConstant.DEFAULT_THUMBS)
                .build();
        remarkThumbsMapper.addThumbs(remarkThumbs);
    }

    /**
     * 分页查询
     *
     * @param remarkPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(RemarkPageQueryDTO remarkPageQueryDTO) {
        PageHelper.startPage(remarkPageQueryDTO.getPage(), remarkPageQueryDTO.getPageSize());
        Page<RemarkPageQueryVO> page = remarkMapper.pageQuery(remarkPageQueryDTO);
        page.forEach(item -> {
            // 查询用户信息
            User user = userMapper.selectById(item.getUserId());
            item.setName(user.getName());
            item.setAvatar(user.getAvatar());
            // 查询点赞情况
            RemarkThumbs remarkThumbs = remarkThumbsMapper.selectByUserIdAndRemarkId(BaseContext.getCurrentId(), item.getId());
            if (remarkThumbs != null) {
                item.setIsThumbs(remarkThumbs.getIsThumbs());
            } else {
                item.setIsThumbs(StatusConstant.DEFAULT_THUMBS);
            }
        });
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除
     *
     * @param id
     */
    @Transactional
    @Override
    public void deleteById(int id) {
        // 删除评论
        remarkMapper.deleteById(id);
        // 删除评论中的赞
        remarkThumbsMapper.deleteThumbsByRemarkId(id);
    }

    /**
     * 赞或踩
     *
     * @param remarkId
     * @param isThumbs
     */
    @Transactional
    @Override
    public void thumbs(int remarkId, int isThumbs) {
        // 赞或踩
        // 判断该评论是否为用户发布，若不是则表示在其他人的评论上赞或踩，要先把本用户的信息添加到表中
        RemarkThumbs rt = remarkThumbsMapper.selectByUserIdAndRemarkId(BaseContext.getCurrentId(), remarkId);
        if (rt == null) {
            // 添加操作
            remarkThumbsMapper.addThumbs(RemarkThumbs.builder()
                    .remarkId(remarkId)
                    .isThumbs(isThumbs)
                    .userId(BaseContext.getCurrentId())
                    .build());
        } else {
            // 修改操作
            RemarkThumbs remarkThumbs = RemarkThumbs.builder()
                    .remarkId(remarkId)
                    .isThumbs(isThumbs)
                    .userId(BaseContext.getCurrentId())
                    .build();
            remarkThumbsMapper.updateThumbs(remarkThumbs);
        }
        // 如果当前状态为1，且传过来的状态为-1，则表示取消赞，数量减一
        if (rt != null && rt.getIsThumbs() != StatusConstant.THUMBS_UP && isThumbs == StatusConstant.DEFAULT_THUMBS) {
            return;
        }
        // 当前状态为0，且传过来的值为-1，则表示已是取消踩的状态，不进行减一
        if(rt != null && rt.getIsThumbs() == StatusConstant.DEFAULT_THUMBS && isThumbs == StatusConstant.THUMBS_DOWN){
            return;
        }
        // 如果第一次rt的为空，则表示用户非评论发布人，但是点击了踩，直接跳出不进行统计
        if(rt == null && isThumbs == StatusConstant.THUMBS_DOWN){
            return;
        }
        // 然后更新评论的总赞数
        Remark remark = remarkMapper.selectById(remarkId);
        // 判断是加一还是减一
        // 如果总赞数为零并且传过来的值为0，则不进行增减操作
        if (remark.getThumbsCount() == 0 && (isThumbs == 0 || isThumbs == -1)) {
            return;
        }
        isThumbs = isThumbs == 1 ? 1 : -1;
        remark.setThumbsCount(remark.getThumbsCount() + isThumbs);
        // 更新评论表中的点赞数
        remarkMapper.update(remark);
    }

}
